package com.ustadzmenjawab.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.ustadzmenjawab.shared.Article;

public class ContentInsertionPage {
	public ContentInsertionPage(){
		final Button sendButton = new Button("Send");
		//final TextBox nameField = new TextBox();
		//nameField.setText("GWT User");
		FlexTable table = new FlexTable();
		
		final Label errorLabel = new Label();
		final Label judulLabel = new Label("Judul");
		final Label ustadzLabel = new Label("Ustadz");
		final Label sumberLabel = new Label("Sumber");
		final Label tahunLabel = new Label("Tahun");
		final Label urlLabel = new Label("URL");
		final Label kategoriLabel = new Label("Kategori");
		final Label artikelLabel = new Label("Artikel");
		
		table.setWidget(0, 0, judulLabel);
		table.setWidget(1, 0, ustadzLabel);
		table.setWidget(2, 0, sumberLabel);
		table.setWidget(3, 0, tahunLabel);
		table.setWidget(4, 0, urlLabel);
		table.setWidget(5, 0, kategoriLabel);
		table.setWidget(6, 0, artikelLabel);
		
		final TextBox judulField = new TextBox();
		judulField.setWidth("100%");
		final TextBox ustadzField = new TextBox();
		ustadzField.setWidth("100%");
		
		
		final TextBox sumberField = new TextBox();
		sumberField.setWidth("100%");
		final TextBox tahunField = new TextBox();
		tahunField.setWidth("100%");
		final TextBox urlField = new TextBox();
		urlField.setWidth("100%");
		final TextBox kategoriField = new TextBox();
		kategoriField.setWidth("100%");
		final RichTextArea artikelField = new RichTextArea();
		final RichTextToolbar artikelToolbar = new RichTextToolbar(artikelField);
		table.setWidget(0,1,judulField);
		table.setWidget(1,1,ustadzField);
		table.setWidget(2,1,sumberField);
		table.setWidget(3,1,tahunField);
		table.setWidget(4,1,urlField);
		table.setWidget(5,1,kategoriField);
		table.setWidget(6,1,artikelToolbar);
		table.setWidget(7,1,artikelField);
		artikelField.setWidth("100%");
		artikelField.setHeight("90%");
		// We can add style names to widgets
		sendButton.addStyleName("sendButton");
		table.setWidget(8, 1, sendButton);
		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get().add(table);
	
		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		
		
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
		
		class InsertArticle implements ClickHandler{

			@Override
			public void onClick(ClickEvent event) {
				AppControlServiceAsync appService = GWT.create(AppControlService.class);
				ServiceDefTarget endpoint = (ServiceDefTarget) appService;
				String moduleRelativeURL = GWT.getModuleBaseURL() + "appControl";
				endpoint.setServiceEntryPoint(moduleRelativeURL);
				
				AsyncCallback callback = new AsyncCallback(){
					@Override
					public void onFailure(Throwable caught) {
						
					}

					@Override
					public void onSuccess(Object result) {
						judulField.setText("");
						urlField.setText("");
						artikelField.setText("");
					}
				};
				Article article = new Article();
				article.setJudul(judulField.getText());
				article.setKategori(kategoriField.getText());
				article.setSumber(sumberField.getText());
				article.setTahun(tahunField.getText());
				article.setUrl(urlField.getText());
				article.setUstadz(ustadzField.getText());
				article.setArtikel(artikelField.getText());
				appService.addNewArticle(article, callback);
			}
			
		}
	// Add a handler to send the name to the server
	//	MyHandler handler = new MyHandler();
		sendButton.addClickHandler(new InsertArticle());
	//	nameField.addKeyUpHandler(handler);
	}
}
